<?php

namespace Biz\service\bizAd;

use Biz\model\bizAd\AdWalletLog;
use Biz\pockets\constInterface\BizAd;
use Biz\pockets\serviceTrait\_Async;
use Doraemon\service\DingTalkAlert;
use Doraemon\tools\HhzEnv;
use Doraemon\tools\Log;

class AdWalletMonitor
{
    use _Async;

    public static function balanceChangeRemind($message)
    {
        if (HhzEnv::isProduction()) {
            try {
                $message['action'] = AdWalletLog::OP_TYPES[$message['action']];

                $opUserType = $message['op_utype'];

                $message['op_utype'] = AdWalletLog::OP_UID_TYPES[$opUserType];

                $message['utype'] = BizAd::AD_UTYPES[$message['utype']] ?? '';

                $text = <<<MSG
- 动账操作：**{$message['action']}**
- UID: **{$message['uid']}**
- 用户类型: **{$message['utype']}**
- 操作金额: **{$message['amount']}**
- 余额: **{$message['balance']}**
- 在途金额: **{$message['lock_amount']}**
- 操作人UID: **{$message['op_uid']}**
- 操作人类型: **{$message['op_utype']}**
- 操作关联数据: **{$message['op_rel_id']}**
- 操作时间: **{$message['time']}**
- 版本号: **{$message['version']}**
MSG;
                $dingMessage = [
                    'access_token' => DingTalkAlert::AD_WALLET_MONITOR,
                    'title' => $message['title'],
                    'text' => $text,
                ];

                self::dingAlert($dingMessage);

            } catch (\Exception $e) {
                Log::error($e->getMessage(), $message);
            }
        }
    }


    public static function bugAlert($message)
    {
        if (HhzEnv::isProduction()) {
            try {
                $text = <<<MSG
- 错误描述：**{$message['error_message']}**
- 错误码：**{$message['error_code']}**
- 调用方法：**{$message['call_method']}**
- 时间: **{$message['time']}**
MSG;
                $dingMessage = [
                    'access_token' => DingTalkAlert::AD_WALLET_TRANS_BUG,
                    'title' => '异常提醒',
                    'text' => $text,
                ];
                self::dingAlert($dingMessage);
            } catch (\Exception $e) {
                Log::error($e->getMessage(), $message);
            }
        }
    }


    /**
     * 异步发送钉钉提醒
     * @param $message
     * @return void
     */
    public static function dingAlert($message)
    {
        //self::_async($message);
        self::_async_dingAlert($message);
    }


    public static function _async_dingAlert($message)
    {
        $accessToken = $message['access_token'];

        $msgTitle = $message['title'];

        $msgText = $message['text'];

        DingTalkAlert::sendMarkdownMsg($accessToken, $msgTitle, $msgText, true);
    }


}